package sm.server.vmOperations;


import java.net.URL;
import com.vmware.vim25.ManagedObjectReference;
import com.vmware.vim25.ServiceContent;
import com.vmware.vim25.VimPortType;
import com.vmware.vim25.VimServiceLocator;



public class LoginAndOut {
	 private ManagedObjectReference _svcRef;
	 private VimServiceLocator _locator;
	 private VimPortType _service;
	 private ServiceContent _sic;
 
	public LoginAndOut(){
	    _svcRef = new ManagedObjectReference();
	    _svcRef.setType("ServiceInstance");
	    _svcRef.set_value("ServiceInstance");
	}
	public void setConnectAndLogin(String hostName, String userName, String password) throws Exception{
	    System.setProperty("axis.socketSecureFactory","org.apache.axis.components.net.SunFakeTrustSocketFactory");
		String url="https://"+hostName+"/sdk/vimService";
		_locator=new VimServiceLocator();
		_locator.setMaintainSession(true);
		_service=_locator.getVimPort(new URL(url));
		_sic=_service.retrieveServiceContent(_svcRef);
		
		if(_sic.getSessionManager()!=null){
			_service.login(_sic.getSessionManager(),userName,password,null);
			System.out.println(_sic.getAbout().getFullName());
			System.out.println("APT type is"+_sic.getAbout().getApiType());
			System.out.println("API version is"+_sic.getAbout().getApiVersion());
		}
	}
	
	public ServiceContent getServiceContent(){
		return this._sic;
	}
	public VimPortType getVimPortType(){
		return this._service;
	}
	public VimServiceLocator getLocator(){
		return this._locator;
	}
	public ManagedObjectReference getMOR(){
		return this._svcRef;
	}
	
	public void logout() throws Exception{
	    _service.logout(_sic.getSessionManager());
	    _service=null;
	    _sic=null;
	}
}
